package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.entity.enums.MapPlace;
import com.mouse.hxsj.entity.enums.MonsterEnum;
import com.mouse.hxsj.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class FindMapService {
    @Autowired
    private FlyService flyService;

//    @Autowired
    private CustomService customService;

    public void find(Com com, MapPlace mapPlace) {
        FindPic findPic = new FindPic(com);
        Press press = new Press(com);
        Mouse mouse = new Mouse(com);
        //飞来飞去
        flyService.fly(com, mapPlace);
        //已到达
        boolean findMapFlag = customService.loop(10, () -> {
            String pointFlag = findPic.findPics(0, 0, 0, 0,
                    "findMap/" + mapPlace.name() + ".bmp", "203040", 0.8, 0);
            log.info("坐标：{}", pointFlag);
            Sleep.sleep(50);
            return StringUtil.isNotBlank(pointFlag);
        });
        log.info("已到达标识:{}", findMapFlag);
        if (!findMapFlag) return;
        //地图跑图
        press.keyPress(Press.M);
        List<Integer> mHwmdList = new ArrayList<>();
        boolean mFlag = customService.loop(3, () -> {
            int[] pic = findPic.findPic(0, 0, 0, 0,
                    "findMap/M标识.bmp", "203040", 0.8, 0);
            if (pic[0] != -1) mHwmdList.addAll(Arrays.asList(pic[1], pic[2]));
            Sleep.sleep(50);
            return pic[0] != -1;
        });
        log.info("按M标识:{}", mFlag);
        if (!mFlag) return;
//        mouse.mouseDoubleClick(point.getX() - 0 + mHwmdList.get(0), point.getY() - 0 + mHwmdList.get(1));
    }

    private List<Integer> checkMonster(Com com, List<MonsterEnum> monsterList) {
        List<String> picPathList = monsterList.stream().map(monster -> monster.getPicPath()).collect(Collectors.toList());
        FindPic findPic = new FindPic(com);
        findPic.findPics(0, 0, 0, 0,
                String.join("|", picPathList), "203040", 0.8, 0);
        return null;
    }
}
